package com.wang.mandkey.activity;

import android.media.SoundPool;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.Toast;

import com.wang.mandkey.R;
import com.wang.mandkey.net.Connecter;
import com.wang.mandkey.net.ConnecterPool;
import com.wang.mandkey.net.INetCallBack;
import com.wang.mandkey.tool.MediaTool;
import com.wang.mandkey.tool.RemoteOperateImpl;
import com.wang.mandkey.tool.Tool;
import com.wang.mandkey.utils.ProjectEnvironment;
import com.wang.mandkey.views.MouseClickView;
import com.wang.mandkey.views.MouseClickView.PressListener;
import com.wang.mandkey.views.PuerMouseView;
import com.wang.mandkey.views.WheelView;
import com.wang.mandkey.views.WheelView.MouseWheelListener;

public class PureMouseActivity extends BaseActivity {

	private PuerMouseView mPuerMouseView;
	private ImageButton mMouseClickViewLeft;
	private ImageButton mMouseClickViewRight;
	private Connecter mConenctor;
	private SeekBar seekBar;
	private RemoteOperateImpl mRemoteOperateImpl;
	private ImageButton mButtonUp, mButtonDown;
	private String tag = PureMouseActivity.this.getClass().getSimpleName();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
		MediaTool.addLoadListener(this);
	}

	private void initView() {
		setContentView(R.layout.puermouse);
		mPuerMouseView = (PuerMouseView) findViewById(R.id.puerMouseView);
		mConenctor = ConnecterPool.getConnectorByKey(ConnecterPool.STRING_CKEY);

		if (null == mConenctor) {
			Toast.makeText(this, R.string.mouse_activity_net,
					Toast.LENGTH_SHORT).show();
		} else {
			mRemoteOperateImpl = new RemoteOperateImpl(mConenctor,
					new INetCallBack() {
						public void OnStart() {
						}

						public void OnIntercepted(String source) {
							Tool.doNetLost(PureMouseActivity.this);
						}

						public void OnFinish() {
						}
					});

			mPuerMouseView.setRemoteOperateImpl(mRemoteOperateImpl);
			mPuerMouseView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {

					Log.i(tag, "PureMouseActivity click");
				}
			});
			ProjectEnvironment.BOOLEAN_LOCK_KEYBOAED = true;
		}

		mMouseClickViewLeft = (ImageButton) findViewById(R.id.mouseViewLeft);

		mMouseClickViewLeft.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (null != mRemoteOperateImpl) {
					mRemoteOperateImpl.mouseDown(-1, -1);
					MediaTool.load(PureMouseActivity.this, R.raw.ken_down);
				}
				if (null != mRemoteOperateImpl) {
					mRemoteOperateImpl.mouseUp(-1, -1);
				}
			}
		});
//		mMouseClickViewLeft.setPressListener(new PressListener() {
//
//			@Override
//			public void onRelease() {
//
//				if (null != mRemoteOperateImpl) {
//					mRemoteOperateImpl.mouseUp(-1, -1);
//				}
//			}
//
//			@Override
//			public void onPresess() {
//				if (null != mRemoteOperateImpl) {
//					mRemoteOperateImpl.mouseDown(-1, -1);
//					MediaTool.load(PureMouseActivity.this, R.raw.ken_down);
//				}
//
//			}
//		});
		mMouseClickViewRight = (ImageButton) findViewById(R.id.mouseViewRight);
		mMouseClickViewRight.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (null != mRemoteOperateImpl) {
					mRemoteOperateImpl.rightClick(-1, -1);
					MediaTool.load(PureMouseActivity.this, R.raw.ken_down);
				}
			}
		});
//		
//		mMouseClickViewRight.setPressListener(new PressListener() {
//
//			@Override
//			public void onRelease() {
//
//			}
//
//			@Override
//			public void onPresess() {
//				if (null != mRemoteOperateImpl) {
//					mRemoteOperateImpl.rightClick(-1, -1);
//					MediaTool.load(PureMouseActivity.this, R.raw.ken_down);
//				}
//			}
//		});
//		mWheelView.setmMouseWheelListener(new MouseWheelListener() {
//			@Override
//			public void OnMouseWheelChange(int change) {
//
//				/*
//				 * mRemoteOperateImpl.mouseWheel(change
//				 * ProjectEnvironment.INT_MOUSE_SENSE);
//				 */
//				if (change > 0) {
//					mRemoteOperateImpl
//							.sendCommand(ProjectEnvironment.STRING_COMMAND_PPT_UP);
//				} else {
//					mRemoteOperateImpl
//							.sendCommand(ProjectEnvironment.STRING_COMMAND_PPT_DOWN);
//				}
//
//				// mRemoteOperateImpl(ProjectEnvironment);
//			}
//		});
		seekBar = (SeekBar) findViewById(R.id.seekBarSense);
		seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}

			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				if (0 != progress) {
					ProjectEnvironment.INT_MOUSE_SENSE = progress;
				}
			}
		});
		mButtonUp = (ImageButton) findViewById(R.id.buttonUp);
		mButtonUp.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				MediaTool.load(PureMouseActivity.this, R.raw.ken_down);
				mRemoteOperateImpl
						.sendCommand(ProjectEnvironment.STRING_COMMAND_PPT_UP);
			}
		});
		mButtonDown = (ImageButton) findViewById(R.id.buttonDown);
		mButtonDown.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				MediaTool.load(PureMouseActivity.this, R.raw.ken_down);
				mRemoteOperateImpl
						.sendCommand(ProjectEnvironment.STRING_COMMAND_PPT_DOWN);
			}
		});
	}

	@Override
	public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
		super.onLoadComplete(soundPool, sampleId, status);
		MediaTool.play(sampleId);
	}
}
